//
//  AppDataBaseManager.m
//  LimitFree
//
//  Created by lijinghua on 16/3/3.
//  Copyright © 2016年 lijinghua. All rights reserved.
//

#import "AppDataBaseManager.h"
#import "FMDatabase.h"

@interface AppDataBaseManager ()

@property(nonatomic)FMDatabase *db;

@end

@implementation AppDataBaseManager

+ (instancetype)sharedInstance{
    static AppDataBaseManager *s_manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        s_manager = [[AppDataBaseManager alloc]init];
    });
    return s_manager;
}

- (NSString*)dbPath
{
    return [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/appinfo.db"];
}

- (id)init{
    if (self = [super init]) {
        self.db = [[FMDatabase alloc]initWithPath:[self dbPath]];
        //无则创建，有责打开
        if ([self.db open]) {
            //创建表
            [self createDataBaseTable];
        }
    }
    return self;
}

- (void)createDataBaseTable{
    //表结构，跟业务逻辑相关，
    NSString *sql = @"create table if not exists appInfo(appId text primary key,name text,desc text,iconUrl text)";
    BOOL bRet = [self.db executeUpdate:sql];
    if (bRet) {
        NSLog(@"创建表结构成功");
    }else{
        NSLog(@"创建表失败");
        [self.db close];
    }
}

- (void)insertAppInfo:(AppDetailModel*)appInfo{
    NSString *sql = @"insert into appInfo values(?,?,?,?)";
    BOOL ret = [self.db executeUpdate:sql,appInfo.applicationId,appInfo.name,appInfo.appDescription,appInfo.iconUrl];
    if (!ret) {
        NSLog(@"插入app 信息失败");
    }
}

- (void)deleteAppInfo:(NSString*)applicationId{
    NSString *sql = @"delete from appInfo where appId = ?";
    BOOL ret = [self.db executeUpdate:sql,applicationId];
    if (!ret) {
        NSLog(@"删除 app 失败");
    }
}

- (void)updateAppInfo:(AppDetailModel*)appInfo{
    NSString *sql = @"update appInfo set name = ?, desc = ?, iconUrl = ? where appId = ?";
    BOOL ret = [self.db executeUpdate:sql,appInfo.name,appInfo.appDescription,appInfo.iconUrl,appInfo.applicationId];
    if (!ret) {
        NSLog(@"更新app失败");
    }
}

- (NSArray*)allApp{
    NSMutableArray *result = [NSMutableArray array];
    NSString *sql = @"select * from appInfo";
    FMResultSet *resultSet = [self.db executeQuery:sql];
    //遍历结果集，每次提取一行，直到提取完返回NO
    while (resultSet.next) {
        AppDetailModel *model = [[AppDetailModel alloc]init];
        model.applicationId = [resultSet stringForColumn:@"appId"];
        model.appDescription = [resultSet stringForColumn:@"desc"];
        model.name = [resultSet stringForColumn:@"name"];
        model.iconUrl = [resultSet stringForColumn:@"iconUrl"];
        
        [result addObject:model];
    }
    [resultSet close];
    return result;
}


- (BOOL)isAppExists:(NSString*)applicationId{
    //根据applicationId 查询表，然后根据返回的结果集中是否包含记录来决定是否存在该app
    NSString *sql =  @"select * from appInfo where appId = ?";
    FMResultSet  *resultSet = [self.db executeQuery:sql,applicationId];
    if (resultSet.next == YES) {
        //存在改记录
        [resultSet  close];
        return YES;
    }else{
        [resultSet close];
        return NO;
    }
}


@end
